/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package demo.controller;

import demo.model.dao.ProductDA;
import demo.model.dto.ProductTO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Admin
 */
public class CreateProductController extends WebController {
    
    public void display(HttpServletRequest request, HttpServletResponse response) throws Exception {
        create(request, response);
        setTitle(LANGUAGE.get("create_product"), request);
        forward("create_product.jsp", request, response);
    }
    
    public void create(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ProductDA productDA;
        ProductTO productTO;
        String act = request.getParameter("act");
        if (validate(request, response) && "create".equals(act)) {
            productDA = new ProductDA(DB_SETTING, SQL_BUNDLE);
            String nameProduct = request.getParameter("name");
            String price = request.getParameter("price");
            String note = request.getParameter("note");
            productTO = new ProductTO();
            productTO.setName(nameProduct);
            productTO.setPrice(Double.parseDouble(price));
            productTO.setNote(note);
            productDA.insertProduct(productTO);
            this.addMessage(LANGUAGE.get("success"));
            redirect("/home/display", response);
        } else if ("create".equals(act)) {
            this.addError(LANGUAGE.get("fail"));
        }
    }

    public Boolean validate(HttpServletRequest request, HttpServletResponse response) {
        String nameProduct = request.getParameter("name");
        String price = request.getParameter("price");
        String note = request.getParameter("note");
        if ("".equals(nameProduct) || "".equals(price) || "".equals(note)) {
            return false;
        }
        return true;
    }
}
